

let alphaTableObserver = null

import { createTableBottomAlpha } from '@/content-scripts/components/trading/alpha/bottomTable.js'

export function observeAlphaTable() {
    if (alphaTableObserver) alphaTableObserver.disconnect();

    let previousValues = {
        firstStageTabs: null
    };
    alphaTableObserver = new MutationObserver(() => {
        const tabBtn = document.querySelector('.mantine-ujq11l > .mantine-q2zxwr[data-active="true"]')
        const tabsInnerHTML = tabBtn.querySelector('.mantine-1d51fly').innerHTML
        // console.log(tabsInnerHTML, 'changeBtnchangeBtnchangeBtnchangeBtnchangeBtn');
        const hasChanged = tabsInnerHTML !== previousValues.firstStageTabs
        // console.log(hasChanged, 'hasChanged');
        if (tabBtn && hasChanged) {
            previousValues = {
                firstStageTabs: tabsInnerHTML
            }
            if (previousValues.firstStageTabs.includes('全网成交')) {
                onlineTransactions(previousValues)
            } else if (previousValues.firstStageTabs.includes('历史记录')) {
                historicalRecords(previousValues)
            } else {
                if (onlinTransactionsObserver) onlinTransactionsObserver.disconnect();
                if (historicalRecordsObserver) historicalRecordsObserver.disconnect();
                createTableBottomAlpha(previousValues)
            }
        }
    });
    alphaTableObserver.observe(document.body, {
        childList: true,
        subtree: true,
        attributes: true,
        characterData: true,
    });
}

let onlinTransactionsObserver = null

function onlineTransactions(value) {
    if (onlinTransactionsObserver) onlinTransactionsObserver.disconnect();
    if (historicalRecordsObserver) historicalRecordsObserver.disconnect();
    let previousValues = {
        directionChange: null
    };
    onlinTransactionsObserver = new MutationObserver(() => {
        const directionInput = document.querySelector('.mantine-1b20kon > div > .mantine-dcm9q0 > .mantine-wz61vw')
        // console.log(directionInput, 'direeeeeeeeeeeeeeeeee');

        const hasChanged = directionInput.value !== previousValues.directionChange

        if (directionInput && hasChanged) {
            previousValues = {
                ...value,
                directionChange: directionInput.value
            }

            createTableBottomAlpha(previousValues)
        }
    });
    onlinTransactionsObserver.observe(document.body, {
        childList: true,
        subtree: true,
        attributes: true,
        characterData: true,
    });
}

let historicalRecordsObserver = null

function historicalRecords(value) {
    if (onlinTransactionsObserver) onlinTransactionsObserver.disconnect();
    if (historicalRecordsObserver) historicalRecordsObserver.disconnect();
    let previousValues = {
        directionChange: null,
        // timeIntervalValue: null,
        startTimeValue: null,
        endTimeValue: null
    };
    historicalRecordsObserver = new MutationObserver(() => {
        const directionInput = document.querySelector('.mantine-1b20kon > div > .mantine-dcm9q0 > .mantine-wz61vw')
        // const timeInterval = document.querySelector('.mantine-1t5amwr .tr-text-c-text-5');
        const startTime = document.querySelector('.mantine-15kxfbj > .mantine-7lvkc8 > .mantine-1626jtb');
        const endTime = document.querySelector('.mantine-15kxfbj > .mantine-7lvkc8 > .mantine-17whd58');

        const hasChanged =
            directionInput.value !== previousValues.directionChange ||
            // timeInterval.textContent !== previousValues.timeIntervalValue ||
            startTime.value !== previousValues.startTimeValue ||
            endTime.value !== previousValues.endTimeValue

        if (directionInput && startTime && endTime && hasChanged) {
            previousValues = {
                ...value,
                directionChange: directionInput.value,
                // timeIntervalValue: timeInterval.textContent,
                startTimeValue: startTime.value,
                endTimeValue: endTime.value
            }
            console.log(previousValues, 'preeeeeeeeeeee');

            createTableBottomAlpha(previousValues)
        }
    });
    historicalRecordsObserver.observe(document.body, {
        childList: true,
        subtree: true,
        attributes: true,
        characterData: true,
    });
}